package com.terracloud.server.dao.model.sql;

import com.terracloud.server.common.data.DeviceGroup;
import com.terracloud.server.common.data.id.CustomerId;
import com.terracloud.server.common.data.id.DeviceGroupId;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.dao.model.BaseSqlEntity;
import com.terracloud.server.dao.model.BaseVersionedEntity;
import com.terracloud.server.dao.model.ModelConstants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.UUID;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

/**
 *
 * @author chenhainan
 * @since 2025/10/11
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = ModelConstants.DEVICE_GROUP_TABLE_NAME)
@NoArgsConstructor
public class DeviceGroupEntity extends BaseSqlEntity<DeviceGroup> {

    @Column(name = ModelConstants.DEVICE_TENANT_ID_PROPERTY, columnDefinition = "uuid")
    private UUID tenantId;

    @Column(name = ModelConstants.DEVICE_NAME_PROPERTY)
    private String name;

    @Column(name = ModelConstants.DEVICE_PROFILE_DESCRIPTION_PROPERTY)
    private String description;

    @Override
    public DeviceGroup toData() {
        final var deviceGroup = new DeviceGroup();
        deviceGroup.setId(new DeviceGroupId(id));
        deviceGroup.setCreatedTime(createdTime);
        deviceGroup.setTenantId(TenantId.fromUUID(tenantId));
        deviceGroup.setName(name);
        deviceGroup.setDescription(description);
        return deviceGroup;
    }

    public DeviceGroupEntity(DeviceGroup deviceGroup) {
        this.setId(deviceGroup.getUuidId());
        this.setCreatedTime(deviceGroup.getCreatedTime());
        this.setName(deviceGroup.getName());
        this.tenantId = deviceGroup.getTenantId().getId();
        this.description = deviceGroup.getDescription();
    }
}
